﻿<!--Usage:
<ClaimsDisplay ClaimType="role" />
-->
@using System.Linq
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication

@inject AuthenticationStateProvider AuthenticationStateProvider

@ClaimType: @Claim

@code {

    [Parameter]
    public string? ClaimType { get; set; }

    public string? Claim { get; set; }

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;
        if (user.Identity != null && user.Identity.IsAuthenticated)
        {
            var userClaims = user.Claims.Where(claim => claim.Type.Equals(ClaimType));
            Claim = userClaims.Any() ? userClaims.Select(claim => claim.Value).Aggregate(AddClaims) : "";
        }
    }

    private static string AddClaims(string left, string right) => left + ", " + right;
}